package org.jzs.mybaseapp.section.home

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Button
import android.widget.ImageView
import butterknife.BindView
import butterknife.ButterKnife
import butterknife.OnClick
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory
import org.jzs.mybaseapp.Applications
import org.jzs.mybaseapp.R
import org.jzs.mybaseapp.common.system.AppConfig
import org.jzs.mybaseapp.common.utils.AppLog
import org.jzs.mybaseapp.common.utils.glide.GlideCatchUtil
import org.jzs.mybaseapp.common.utils.glide.GlideUtils
import android.support.v4.app.ActivityOptionsCompat
import android.content.Intent
import android.widget.Toast
import android.os.Build
import android.view.View
import com.bumptech.glide.load.resource.drawable.GlideDrawable
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import org.jzs.mybaseapp.common.retorift.RequestCallBack
import org.jzs.mybaseapp.common.system.Constant
import org.jzs.mybaseapp.common.utils.glide.GlideUtils.showImgLister
import java.lang.Exception


class GlideActivity : AppCompatActivity() {

    @BindView(R.id.iv1)
    lateinit var iv1: ImageView
    @BindView(R.id.iv2)
    lateinit var iv2: ImageView
    @BindView(R.id.iv3)
    lateinit var iv3: ImageView
    @BindView(R.id.iv4)
    lateinit var iv4: ImageView
    @BindView(R.id.iv5)
    lateinit var iv5: ImageView
    @BindView(R.id.iv6)
    lateinit var iv6: ImageView
    @BindView(R.id.iv7)
    lateinit var iv7: ImageView
    @BindView(R.id.btn)
    lateinit var btn: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_glide)
        ButterKnife.bind(this)

        btn!!.setText(GlideCatchUtil.getInstance().getCacheSize(this@GlideActivity))

        AppLog.e(AppConfig.APP_SDCARD_DIR)
        AppLog.e(getCacheDir().toString() + "/" + InternalCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR)
    }


    @OnClick(R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4, R.id.btn5, R.id.btn6, R.id.btn7, R.id.iv1, R.id.iv2, R.id.iv3, R.id.iv4, R.id.iv5, R.id.iv6, R.id.iv7)
    internal fun iv(v: View) {
        when (v.id) {
            R.id.btn1 -> GlideUtils.showImgAnimate(Applications.context(), "https://www.baidu.com/img/bd_logo1.png", iv1)
            R.id.btn2 -> GlideUtils.showImgNoAnimate(Applications.context(), "https://www.baidu.com/img/bd_logo1.png", iv2)
            R.id.btn3 -> {
                showImgLister(Applications.context(), "http://ssyer.oss-cn-hangzhou.aliyuncs.com/user//20170802/order/a18ae434f33a3f4660f80a1facab6720317e36jpg.jpg", iv3,
                        object : RequestListener<String, GlideDrawable> {
                            override fun onException(e: Exception?, model: String?, target: Target<GlideDrawable>?, isFirstResource: Boolean): Boolean {
                                return false
                            }

                            override fun onResourceReady(resource: GlideDrawable?, model: String?, target: Target<GlideDrawable>?, isFromMemoryCache: Boolean, isFirstResource: Boolean): Boolean {
                                return false
                            }

                        })
            }
            R.id.btn4 -> GlideUtils.showImgSimple(Applications.context(), "https://www.baidu.com/img/bd_logo1.png", iv4, 45)
            R.id.btn5 -> GlideUtils.showImgSimple(Applications.context(), "https://www.baidu.com/img/bd_logo1.png", iv5, 90)
            R.id.btn6 -> GlideUtils.showImgAnimate(Applications.context(), "http://ssyer.oss-cn-hangzhou.aliyuncs.com/user//20170802/order/a18ae434f33a3f4660f80a1facab6720317e36jpg.jpg", iv6)
            R.id.btn7 -> GlideUtils.showImgAnimate(Applications.context(), "https://www.baidu.com/img/bd_logo12.png", iv7)
            R.id.btn -> {
                GlideCatchUtil.getInstance().clearCacheDiskSelf()
                btn!!.setText(GlideCatchUtil.getInstance().getCacheSize())
            }
            else -> {
                if (Build.VERSION.SDK_INT < 21) {
                    Toast.makeText(this@GlideActivity, "21+ only, keep out", Toast.LENGTH_SHORT).show()
                } else {
                    val intent = Intent(this@GlideActivity, ActivityTransitionToActivity::class.java)
                    intent.putExtra(Constant.EXTRA.EXTRA_ITEM, "http://ssyer.oss-cn-hangzhou.aliyuncs.com/user//20170802/order/a18ae434f33a3f4660f80a1facab6720317e36jpg.jpg")
                    val options = ActivityOptionsCompat.makeSceneTransitionAnimation(this@GlideActivity, v, "transition")
                    startActivity(intent, options.toBundle())
                }
            }
        }
    }
}
